#include <iostream>
#include "json/json.h"
#include "json/jsoncpp/JsonMap.h"
#include "File.hpp"

using namespace std;

string map2jsonstr(const map<string, string> &map_info)
{
    Json::Value jObject;
    for (map<string, string>::const_iterator iter = map_info.begin(); iter != map_info.end(); ++iter)
    {
        jObject[iter->first] = iter->second;
    }
    return jObject.toStyledString();
}

string itoa_self(int i)
{
    stringstream ss;
    ss << i;
    return ss.str();
}

map<string, string> jsonstr2map(const string &json)
{
    Json::Reader reader;
    Json::Value value;
    map<string, string> maps;

    if (json.length() > 0)
    {
        if (reader.parse(json, value))
        {
            Json::Value::Members members = value.getMemberNames();
            for (Json::Value::Members::iterator it = members.begin(); it != members.end(); it++)
            {
                Json::ValueType vt = value[*it].type();
                switch (vt)
                {
                    case Json::stringValue:
                    {
                        maps.insert(pair<string, string>(*it, value[*it].asString()));
                        break;
                    }
                    case Json::intValue:
                    {
                        int intTmp = value[*it].asInt();
                        maps.insert(pair<string, string>(*it, itoa_self(intTmp)));
                        break;
                    }
                    case Json::arrayValue:
                    {
                        std::string strid;
                        for (unsigned int i = 0; i < value[*it].size(); i++)
                        {
                            strid += value[*it][i].asString();
                            strid += ",";
                        }
                        if (!strid.empty())
                        {
                            strid = strid.substr(0, strid.size() - 1);
                        }
                        maps.insert(pair<string, string>(*it, strid));
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }//end switch
            }//end for
        }//end if
    }
    return maps;
}

int main()
{
    string fileName = "test.json";
    sh::File file(fileName);
    file.open(std::ios_base::in);
    string jsonStr = file.readAll();

    Json::CharReaderBuilder builder;
    Json::CharReader *reader = builder.newCharReader();

    Json::Value root;
    JSONCPP_STRING error;
    if (!reader->parse(jsonStr.c_str(), jsonStr.c_str() + jsonStr.length(), &root, &error))
    {
        cout << error << endl;
        delete reader;
        return 0;
    }
    delete reader;
    cout << root.type() << endl;
    cout << root["1"].asString() << endl;
    cout << root["name"].asString() << endl;
    cout << root["age"] << endl;
    Json::Value bags = root["bags"];
    cout << bags.type() << endl;
    cout << bags.get(4, "null") << endl;
//    map<string, string> str = jsonstr2map(jsonStr);
//    cout << str.at("root") << endl;
//    string bags = str.at("bags");
//    cout << bags << endl;
//
//    map<string, string> bagsMap = jsonstr2map(bags);

    cout << "----------------------" << endl;
    map<string, int> data;
    data.insert(make_pair("a", 2));

    std::map<char, int> mymap;
    mymap['a'] = 101;
    mymap['b'] = 202;
    mymap['c'] = 302;

    std::map<char, std::map<char, int> > mymap2;
    mymap2['d'] = mymap;
    mymap2['e'] = mymap;

    std::cout << "mymap2.size() is " << mymap2.size() << '\n';

    return 0;
}
